#!/bin/sh

#url="http://test.ngrok.msoa.top:56080/other/version"
url="https://apiservice.msoa.top/other/version"

# web
# 下载代码 账户名如果有@符号，则需要改成：%40 进行转移
git clone https://$GITUSER_L:$GITPWD_L@gitee.com/lanfeng9437/identity-authentication-platform.git
cd identity-authentication-platform || exit

DOCKER_ADDRESS="172.26.51.74:5000"
DOCKER_TAG=""

# manage
TAG=$(git describe --tags $(git rev-list --tags --all --grep='release: @iap/manage' --max-count=1))
echo ${TAG}
WEB_VERSION=${TAG#*v}
echo ${WEB_VERSION}
bool_web=$(curl "${url}?flag=iap-manage-web&last=${WEB_VERSION}")
if [ $bool_web == 'true' ]; then
    DOCKER_TAG="${DOCKER_ADDRESS}/iap-manage-web"
    echo "开始编译 @iap/manage v${WEB_VERSION} ${DOCKER_TAG}"
    docker build -f ./packages/manage/Dockerfile -t ${DOCKER_TAG}:${WEB_VERSION} -t ${DOCKER_TAG}:latest .
    docker push ${DOCKER_TAG}:${WEB_VERSION}
    docker push ${DOCKER_TAG}:latest
    docker rmi ${DOCKER_TAG}:${WEB_VERSION}
    docker rmi ${DOCKER_TAG}:latest
fi


######################### 服务编译 #########################
cd ..

# 下载代码
git clone https://$GITUSER_M:$GITPWD_M@gitee.com/misu99/identity-authentication-service.git
ls -l

# 编译
cd identity-authentication-service || exit

tagIdentity=$(git describe --tags $(git rev-list --tags --all --grep='release: @app/identity' --max-count=1))
tagAsynctask=$(git describe --tags $(git rev-list --tags --all --grep='release: @app/asynctask' --max-count=1))
tagComparison=$(git describe --tags $(git rev-list --tags --all --grep='release: @app/comparison' --max-count=1))
tagScheduler=$(git describe --tags $(git rev-list --tags --all --grep='release: @app/scheduler' --max-count=1))

verIdentity=${tagIdentity#*v}
verAsynctask=${tagAsynctask#*v}
verComparison=${tagComparison#*v}
verScheduler=${tagScheduler#*v}

# 比对版本
bool_identity=$(curl "${url}?flag=identity-authentication-service&last=${verIdentity}")
bool_asynctask=$(curl "${url}?flag=identity-asynctask-service&last=${verAsynctask}")
bool_comparison=$(curl "${url}?flag=identity-comparison-service&last=${verComparison}")
bool_scheduler=$(curl "${url}?flag=identity-scheduler-service&last=${verScheduler}")

if [ $bool_identity == 'true' ]; then
    echo "${bool_identity} 开始编译 identity-service"
    docker build -f deploy/release/Dockerfile -t 172.26.51.74:5000/identity-authentication-service -t 172.26.51.74:5000/identity-authentication-service:${verIdentity} --target IdentityServer .
    docker push 172.26.51.74:5000/identity-authentication-service:${verIdentity}
    docker push 172.26.51.74:5000/identity-authentication-service
    docker rmi 172.26.51.74:5000/identity-authentication-service:${verIdentity}
    docker rmi 172.26.51.74:5000/identity-authentication-service
fi
if [ $bool_asynctask == 'true' ]; then
    echo "${bool_asynctask} 开始编译 asynctask-service"
    docker build -f deploy/release/Dockerfile -t 172.26.51.74:5000/identity-asynctask-service -t 172.26.51.74:5000/identity-asynctask-service:${verAsynctask} --target AsyncTask .
    docker push 172.26.51.74:5000/identity-asynctask-service:${verAsynctask}
    docker push 172.26.51.74:5000/identity-asynctask-service
    docker rmi 172.26.51.74:5000/identity-asynctask-service:${verAsynctask}
    docker rmi 172.26.51.74:5000/identity-asynctask-service
fi
if [ $bool_comparison == 'true' ]; then
    echo "${bool_comparison} 开始编译 comparison-service"
    docker build -f deploy/release/Dockerfile -t 172.26.51.74:5000/identity-comparison-service -t 172.26.51.74:5000/identity-comparison-service:${verComparison} --target ComparisonServer .
    docker push 172.26.51.74:5000/identity-comparison-service:${verComparison}
    docker push 172.26.51.74:5000/identity-comparison-service
    docker rmi 172.26.51.74:5000/identity-comparison-service:${verComparison}
    docker rmi 172.26.51.74:5000/identity-comparison-service
fi
if [ $bool_scheduler == 'true' ]; then
    echo "${bool_scheduler} 开始编译 scheduler-service"
    docker build -f deploy/release/Dockerfile -t 172.26.51.74:5000/identity-scheduler-service -t 172.26.51.74:5000/identity-scheduler-service:${verScheduler} --target SchedulerServer .
    docker push 172.26.51.74:5000/identity-scheduler-service:${verScheduler}
    docker push 172.26.51.74:5000/identity-scheduler-service
    docker rmi 172.26.51.74:5000/identity-scheduler-service:${verScheduler}
    docker rmi 172.26.51.74:5000/identity-scheduler-service
fi
